package com.mission.game.components;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mission.game.R;

public class StatusAdapter<T extends Statusable> extends ArrayAdapter<T> {
	Context _context;
	int _layoutResourceId;
	ArrayList<? extends Statusable> _data;

	public StatusAdapter(Context context, int layoutResourceId,
			ArrayList<T> data) {
		super(context, layoutResourceId, data);
		_layoutResourceId = layoutResourceId;
		_context = context;
		_data = data;
	}

	static class StatusHolder {
		ImageView statusIcon;
		TextView text;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		StatusHolder holder = null;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(_layoutResourceId, parent, false);

			holder = new StatusHolder();
			holder.statusIcon = (ImageView) v.findViewById(R.id.status_icon);
			holder.text = (TextView) v.findViewById(R.id.status_text);

			v.setTag(holder);
		} else {
			holder = (StatusHolder) v.getTag();
		}
		holder.text.setText(_data.get(position).getName() + "\n"+_data.get(position).getScore()+"p");
		holder.statusIcon.setImageResource(_data.get(position).getStatusImg());
		return v;
	}
}